package com.itany.netclass.service.proxy;

import com.itany.netclass.entity.Comment;
import com.itany.netclass.exception.NoQuanXianException;
import com.itany.netclass.exception.NullSringException;
import com.itany.netclass.exception.ServiceException;
import com.itany.netclass.factory.ObjectFactory;
import com.itany.netclass.service.CommentService;
import com.itany.netclass.tran.TransactionManager;

import java.util.List;

/**
 * Author:sjy@itany.com
 * Date:2021/10/18 21:52
 * Version:1.0-SNAPSHOT
 */
public class CommentServiceProxy implements CommentService {
    @Override
    public List<Comment> findCommentByChapterId(String chapterId) throws ServiceException, NullSringException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        CommentService commentService = (CommentService) ObjectFactory.getObject("CommentServiceTarget");
        try {
            tran.begin();
            List<Comment> list = commentService.findCommentByChapterId(chapterId);
            tran.commit();
            return list;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (NullSringException e) {
            e.printStackTrace();
            tran.rollback();
            throw e;
        }
    }

    @Override
    public List<Comment> searchCommentByForm(String chapterId,String context, String nickname, String kaishitime, String jieshutime, String status) throws NullSringException, ServiceException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        CommentService commentService = (CommentService) ObjectFactory.getObject("CommentServiceTarget");
        try {
            tran.commit();
            List<Comment> L=commentService.searchCommentByForm(chapterId,context,nickname,kaishitime,jieshutime,status);
            tran.commit();
            return L;
        } catch (NullSringException e) {
            tran.rollback();;
            throw e;
        } catch (ServiceException e) {
            e.printStackTrace();
            tran.rollback();;
            throw e;
        }
    }

    @Override
    public void yongCommentByComment(String commentId, String status) throws ServiceException, NoQuanXianException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        CommentService commentService = (CommentService) ObjectFactory.getObject("CommentServiceTarget");
        try {
            tran.begin();
            commentService.yongCommentByComment(commentId,status);
            tran.commit();
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (NoQuanXianException e) {
            tran.rollback();
            throw e;
        }

    }

    @Override
    public List<Comment> findManageComment() throws ServiceException, NullSringException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        CommentService commentService = (CommentService) ObjectFactory.getObject("CommentServiceTarget");
        try {
            tran.begin();
            List<Comment> manageComment = commentService.findManageComment();
            tran.commit();
            return  manageComment;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (NullSringException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public List<Comment> searchManageByForm(String commentUserName, String context, String kaishitime, String jieshutime) throws ServiceException, NullSringException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        CommentService commentService = (CommentService) ObjectFactory.getObject("CommentServiceTarget");
        try {
            tran.begin();
            List<Comment> list = commentService.searchManageByForm(commentUserName, context, kaishitime, jieshutime);
            tran.commit();
            return list;
        } catch (NullSringException e) {
           tran.rollback();
           throw e;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public void yongManageByCommentId(String commentId, String status) throws ServiceException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        CommentService commentService = (CommentService) ObjectFactory.getObject("CommentServiceTarget");
        try {
            tran.begin();
            commentService.yongManageByCommentId(commentId,status);
            tran.commit();
        } catch (ServiceException e) {
            e.printStackTrace();
            throw e;
        }
    }
}
